package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;
import rad.framework.uml2.uml.edit.providers.ElementItemProvider;

public class NamedElementItemProvider
		extends ElementItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public NamedElementItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addNamePropertyDescriptor(object);
			addVisibilityPropertyDescriptor(object);
			addQualifiedNamePropertyDescriptor(object);
			addClientDependencyPropertyDescriptor(object);
			addNamespacePropertyDescriptor(object);
			addNameExpressionPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addNamePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_NamedElement_name_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_NamedElement_name_feature", "_UI_NamedElement_type"),
				UMLPackage.Literals.NAMED_ELEMENT__NAME, true, ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
	}

	protected void addVisibilityPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_NamedElement_visibility_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_NamedElement_visibility_feature", "_UI_NamedElement_type"),
				UMLPackage.Literals.NAMED_ELEMENT__VISIBILITY, true, ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
	}

	protected void addQualifiedNamePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_NamedElement_qualifiedName_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_NamedElement_qualifiedName_feature", "_UI_NamedElement_type"),
				UMLPackage.Literals.NAMED_ELEMENT__QUALIFIED_NAME, false, ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addClientDependencyPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_NamedElement_clientDependency_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_NamedElement_clientDependency_feature", "_UI_NamedElement_type"),
				UMLPackage.Literals.NAMED_ELEMENT__CLIENT_DEPENDENCY, true, null, null, null));
	}

	protected void addNamespacePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_NamedElement_namespace_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_NamedElement_namespace_feature", "_UI_NamedElement_type"),
				UMLPackage.Literals.NAMED_ELEMENT__NAMESPACE, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addNameExpressionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_NamedElement_nameExpression_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_NamedElement_nameExpression_feature", "_UI_NamedElement_type"),
				UMLPackage.Literals.NAMED_ELEMENT__NAME_EXPRESSION, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	@Override
	public Collection getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(UMLPackage.Literals.NAMED_ELEMENT__NAME_EXPRESSION);
		}
		return childrenFeatures;
	}

	@Override
	public String getText(Object object) {
		final String label = ((NamedElement) object).getName();
		return label == null || label.length() == 0 ? getString("_UI_NamedElement_type") :
				getString("_UI_NamedElement_type") + " " + label;
	}

	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(NamedElement.class)) {
			case UMLPackage.NAMED_ELEMENT__NAME:
			case UMLPackage.NAMED_ELEMENT__VISIBILITY:
			case UMLPackage.NAMED_ELEMENT__QUALIFIED_NAME:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case UMLPackage.NAMED_ELEMENT__NAME_EXPRESSION:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	@Override
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

	}

	@Override
	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

}
